using System;
using System.Collections.Generic;
using System.Text;
using VSSDKAssist.Assets.Actions.Base;
using System.Diagnostics;
using Microsoft.Practices.RecipeFramework;
using System.IO;

namespace VSSDKAssist.Assets.Actions.General
{
	/// <summary>
	/// Action that executes a process
	/// </summary>
	public class ExecuteProcessAction : ActionBase
	{
		private string fileName;

		/// <summary>
		/// Gets or sets the name of the file.
		/// </summary>
		/// <value>The name of the file.</value>
		[Input(Required = true)]
		public string FileName
		{
			get { return fileName; }
			set { fileName = value; }
		}

		private string arguments;

		/// <summary>
		/// Gets or sets the arguments.
		/// </summary>
		/// <value>The arguments.</value>
		[Input(Required = true)]
		public string Arguments
		{
			get { return arguments; }
			set { arguments = value; }
		}

		private bool createWindow;

		/// <summary>
		/// Gets or sets a value indicating whether [create window].
		/// </summary>
		/// <value><c>true</c> if [create window]; otherwise, <c>false</c>.</value>
		[Input(Required = false)]
		public bool CreateWindow
		{
			get { return createWindow; }
			set { createWindow = value; }
		}

		private ProcessWindowStyle windowStyle = ProcessWindowStyle.Hidden;

		/// <summary>
		/// Gets or sets the window style.
		/// </summary>
		/// <value>The window style.</value>
		[Input(Required = false)]
		public ProcessWindowStyle WindowStyle
		{
			get { return windowStyle; }
			set { windowStyle = value; }
		}

		private bool waitForExit = true;

		/// <summary>
		/// Gets or sets a value indicating whether [wait for exit].
		/// </summary>
		/// <value><c>true</c> if [wait for exit]; otherwise, <c>false</c>.</value>
		[Input(Required = false)]
		public bool WaitForExit
		{
			get { return waitForExit; }
			set { waitForExit = value; }
		}

		/// <summary>
		/// Called when [execute].
		/// </summary>
		protected override void OnExecute()
		{
			if(File.Exists(this.fileName))
			{
				if(this.fileName.Contains(" "))
				{
					this.fileName = string.Concat("\"", this.fileName, "\"");
				}

				Process process = new Process();
				process.StartInfo.FileName = this.fileName;
				process.StartInfo.Arguments = this.arguments;
				process.StartInfo.CreateNoWindow = this.createWindow;
				process.StartInfo.WindowStyle = this.windowStyle;
				process.Start();

				if(this.waitForExit)
				{
					process.WaitForExit();
				}
			}
		}
	}
}